#include "global.h"

void InitTMR1 (void) // i want a 2.5ms sec interrupt  i have a 1us - so i need count up to 2500us or 2500 with 1:1 prescaller
{	T1CON = 0b00000001; //1:1 PRESCALLER, TIMER ON
	TMR1IF=0;
	TMR1IE=1;		// enable T1 interrupt
	PEIE=1;			// enable peripherial interrupt
};

void TMR1Preload (unsigned int value)
{	
//	TMR1H=value>>8;		//
//	TMR1L=value;		//
	unsigned char lbyte, hbyte;
	lbyte=(char)value ;
	hbyte=(char)(value >> 8);
	TMR1H=hbyte;		//
	TMR1L=lbyte;		//
};

bank1 unsigned char time[9];

unsigned char* ShowLTime(unsigned int seconds,unsigned char* tbuffer)  //00:00:00
{	//int st_temp;
	const int SECSINTENHOURS=36000;
	const int SECSINHOUR=3600;
	const int SECSINTENMINUTES=600;
	const int SECSINMINUTE=60;
	unsigned char digit=0;

	while (seconds >= SECSINTENHOURS)
	{	seconds-=SECSINTENHOURS;
		digit++;
	};
	
		tbuffer[0]=0x30+digit;
		digit=0;
	
	while (seconds >= SECSINHOUR)
	{	seconds-=SECSINHOUR;
		digit++;
	};
		tbuffer[1]=0x30+digit;
		digit=0;

	tbuffer[2]=':';
	while (seconds >= SECSINTENMINUTES)
	{	seconds-=SECSINTENMINUTES;
		digit++;
	};
	
		tbuffer[3]=0x30+digit;
		digit=0;
	
	
	while (seconds >= SECSINMINUTE)
	{	seconds-=SECSINMINUTE;
		digit++;
	};
	
		tbuffer[4]=0x30+digit;
		digit=0;
	tbuffer[5]=':';

	while (seconds >9)
	{	seconds-=10;
		digit++;
	};
		tbuffer[6]=0x30+digit;
		digit=0;

	tbuffer[7]=0x30+seconds;
	tbuffer[8]=0;
	return tbuffer;
}



unsigned char* ShowSTime(unsigned int seconds,unsigned char* tbuffer) // 000:00
{	//int st_temp;

	const int SECSINHUNDREDMINUTES=6000;
	const int SECSINTENMINUTES=600;
	const int SECSINMINUTE=60;
	unsigned char digit=0;

	if (seconds >= SECSINHUNDREDMINUTES)
	{	while (seconds >= SECSINHUNDREDMINUTES)
		{	seconds-=SECSINHUNDREDMINUTES;
			digit++;
		};
		tbuffer[0]=0x30+digit;
		digit=0;
	}
	else
	tbuffer[0]=' ';		
	while (seconds >= SECSINTENMINUTES)
	{	seconds-=SECSINTENMINUTES;
		digit++;
	};
	
		tbuffer[1]=0x30+digit;
		digit=0;
	
	
	while (seconds >= SECSINMINUTE)
	{	seconds-=SECSINMINUTE;
		digit++;
	};
	
		tbuffer[2]=0x30+digit;
		digit=0;
	tbuffer[3]=':';

	while (seconds >9)
	{	seconds-=10;
		digit++;
	};
		tbuffer[4]=0x30+digit;
		digit=0;

	tbuffer[5]=0x30+seconds;
	tbuffer[6]=tbuffer[7]=tbuffer[8]=0;
	return tbuffer;
}

unsigned char* ShowCurrent(unsigned char sc_int,unsigned char* tbuffer) // 000:00
{	unsigned char sc_temp=0;
	sc_int*=2;
	if (sc_int >=100 )
	{	while (sc_int>=100)
		{	sc_int-=100;
			sc_temp+=1;
		};
		tbuffer[0]=0x30+sc_temp;
		sc_temp=0;
	}
	else
	{	tbuffer[0]=' ';
	};

	while (sc_int>=10)
		{	sc_int-=10;
			sc_temp+=1;
		};
		tbuffer[1]=0x30+sc_temp;
		sc_temp=0;

		tbuffer[2]='.';
		tbuffer[3]=0x30+sc_int;
		tbuffer[4]=tbuffer[5]=0;
	return tbuffer;
}


//======================================================================================


